﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace LFractal
{
	/// <summary>
	/// Interaction logic for LFractalMainWindow.xaml
	/// </summary>
	public partial class LFractalMainWindow : Window
	{
        public static string StorageFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\LFractal\";
        private FileSystemWatcher oWatcher = null;


        static LFractalMainWindow()
        {
            DirectoryInfo oDir = new DirectoryInfo(StorageFolder);
            if (!oDir.Exists)
            {
                oDir.Create();
            }
        }

		public LFractalMainWindow()
		{
            this.InitializeComponent();

            /* we check for upgrade availability first */
            if (Updater.SelfUpdateManager.CheckAndUpgrade())
            {
                this.Close();
            }			
			
			// Insert code required on object creation below this point.
            this.Loaded += new RoutedEventHandler(LFractalMainWindow_Loaded);
            this.Closing += new System.ComponentModel.CancelEventHandler(LFractalMainWindow_Closing);
		}

        void LFractalMainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            
        }

        void LFractalMainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshMyWorkList(StorageFolder);
            //oWatcher = new FileSystemWatcher(StorageFolder);
            //oWatcher.Changed += new FileSystemEventHandler(oWatcher_Changed);
            //oWatcher.EnableRaisingEvents = true;
            //oWatcher.Filter = "*.xml";
            //oWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        }

        //void oWatcher_Changed(object sender, FileSystemEventArgs e)
        //{
        //    RefreshMyWorkList();
        //}

        public void RefreshMyWorkList(string storageFolder)
        {
            DirectoryInfo oDir = new DirectoryInfo(storageFolder);
            FileInfo[] oFiles = oDir.GetFiles("*.xml");
            if (oFiles != null && oFiles.Length > 0)
            {
                listOfMyWork.ItemsSource = oFiles;
            }
        }

		private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			this.DragMove();
		}

		private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			this.Close();
		}

		private void btnAddEntity_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			LEntityEditor oEditor = new LEntityEditor(this);
			oEditor.Show();
		}

        private void btnDeleteEntity_Click(object sender, RoutedEventArgs e)
        {
            if (listOfMyWork.SelectedItems != null && listOfMyWork.SelectedItems.Count > 0)
            {
                foreach (FileInfo file in listOfMyWork.SelectedItems)
                {
                    try
                    {
                        file.Delete();
                    }
                    catch (Exception ex)
                    {
                        // Don't care
                    }
                }

                RefreshMyWorkList(StorageFolder);
                
            }
        }

        private void listOfMyWork_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (listOfMyWork.SelectedItem != null)
            {
                FileInfo oFile = (FileInfo) listOfMyWork.SelectedItem ;
                LFractalLib.Entity E = LFractalLib.Entity.FromStream(oFile.FullName);
                LEntityRenderer oRenderer = new LEntityRenderer(E, null, this);
                oRenderer.Show();
                oRenderer.DrawEntity();
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            string localSampleFolder = System.Reflection.Assembly.GetExecutingAssembly().Location;
            localSampleFolder = localSampleFolder.Substring(0,localSampleFolder.LastIndexOf(@"\") + 1);
            localSampleFolder += @"Samples\";

            RefreshMyWorkList(localSampleFolder);

        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
	}
}